package org.farm2.view.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.farm2.base.db.FarmDbFields;
import org.farm2.base.domain.FarmUserContextLoader;
import org.farm2.base.exception.FarmAppException;
import org.farm2.base.exception.FarmExceptionUtils;
import org.farm2.base.password.FarmPasswordEncoder;
import org.farm2.files.domain.ResourceFile;
import org.farm2.files.domain.ex.FarmFilePath;
import org.farm2.files.service.ResourceFileServiceInter;
import org.farm2.files.utils.Farm2RegisteTypeEnum;
import org.farm2.skc.domain.SkcKnowFile;
import org.farm2.skc.enums.KnowFileTypeEnum;
import org.farm2.tools.db.commons.DBRule;
import org.farm2.tools.db.commons.DBRuleList;
import org.farm2.tools.files.Farm2FileUtils;
import org.farm2.tools.i18n.string.FarmStringUtils;
import org.farm2.view.dao.GraphPartDao;
import org.farm2.view.domain.GraphDto;
import org.farm2.view.domain.GraphPart;
import org.farm2.view.service.GraphPartServiceInter;
import org.farm2.tools.bean.FarmBeanUtils;
import org.farm2.tools.db.DataQuery;
import org.farm2.tools.db.DataResult;
import org.farm2.tools.i18n.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**图组件 
 * @author cbtg自动生成  2025-8-29 9:10:02 
 */
@Service
@Slf4j
public class GraphPartServiceImpl implements GraphPartServiceInter {
    @Autowired
    private GraphPartDao graphPartDao;
    @Autowired
    private ResourceFileServiceInter resourceFileServiceImpl;

    @Transactional
    @Override
    public GraphPart insertGraphPartEntity(GraphPart graphPart) {
        FarmDbFields.initInsertBean(graphPart, FarmUserContextLoader.getCurrentUser());
        //FarmBeanUtils.runFunctionByBlank(graphPart.getType(), "1", graphPart::setType);
        graphPartDao.insert(graphPart);
       //[tree：树形结构使用]
       //initTreeCode(actions.getId());
        return graphPart;
    }

    @Transactional
    @Override
    public GraphPart editGraphPartEntity(GraphPart graphPart) {
        GraphPart saveGraphPart = getGraphPartById(graphPart.getId());
        FarmExceptionUtils.throwNullEx(saveGraphPart, I18n.msg("图组件不存在:?", graphPart.getId()));
        saveGraphPart.setId(graphPart.getId());
        saveGraphPart.setCtime(graphPart.getCtime());
        saveGraphPart.setEtime(graphPart.getEtime());
        saveGraphPart.setEuserkey(graphPart.getEuserkey());
        saveGraphPart.setCuserkey(graphPart.getCuserkey());
        saveGraphPart.setState(graphPart.getState());
        saveGraphPart.setNote(graphPart.getNote());
        saveGraphPart.setName(graphPart.getName());
        saveGraphPart.setDatafileid(graphPart.getDatafileid());
        saveGraphPart.setViewimgid(graphPart.getViewimgid());
        saveGraphPart.setAppmodel(graphPart.getAppmodel());
        saveGraphPart.setAppid(graphPart.getAppid());
         
        FarmDbFields.initUpdateBean(saveGraphPart, FarmUserContextLoader.getCurrentUser());
        graphPartDao.update(saveGraphPart);
        return saveGraphPart;
    }

    @Transactional
    @Override
    public GraphPart getGraphPartById(String id) {
        return graphPartDao.findById(id);
    }

    @Override
    public List<GraphPart> getGraphParts(DataQuery query) {
        return graphPartDao.queryData(query.setCount(false)).getData(GraphPart.class);
    }



    @Transactional
    @Override
    public DataResult searchGraphPart(DataQuery query) {
        DataResult result = graphPartDao.queryData(query);
        return result;
    }

    @Override
    public int getGraphPartNum(DataQuery query) {
        return graphPartDao.countData(query);
    }


    @Transactional
    @Override
    public void delGraphPart(String id) {
        GraphPart part = graphPartDao.findById(id);
        resourceFileServiceImpl.cancel(part.getDatafileid(), Farm2RegisteTypeEnum.GRAPH, part.getId());
        resourceFileServiceImpl.cancel(part.getViewimgid(), Farm2RegisteTypeEnum.GRAPH, part.getId());
        graphPartDao.deleteById(id);
    }
    
    @Override
    public int getNum(DataQuery query) {
        return  graphPartDao.countData(query);
    }

    @Transactional
    @Override
    public GraphPart addGraph(GraphDto graph) {
        graph.setTitle("图-" + graph.getTitle());
        String title = graph.getTitle().length() > 64 ? graph.getTitle().substring(0, 64) : graph.getTitle();
        ResourceFile jsonFile = resourceFileServiceImpl.saveFile(title + ".json", graph.getDataJson());
        ResourceFile imgFile = resourceFileServiceImpl.saveImgByBase64(title + ".png", graph.getImgBase64());
        GraphPart part = new GraphPart();
        part.setAppid(graph.getAppid());
        part.setAppmodel(graph.getModel());
        part.setDatafileid(jsonFile.getId());
        part.setViewimgid(imgFile.getId());
        part.setName(title);
        insertGraphPartEntity(part);
        resourceFileServiceImpl.submit(jsonFile.getId(), Farm2RegisteTypeEnum.GRAPH, part.getId());
        resourceFileServiceImpl.submit(imgFile.getId(), Farm2RegisteTypeEnum.GRAPH, part.getId());
        return part;
    }

    @Override
    public String getDataJson(String datafileid) {
        ResourceFile file = resourceFileServiceImpl.getResourceFileById(datafileid);
        FarmFilePath filePath = resourceFileServiceImpl.getFilePath(resourceFileServiceImpl.getResourceFileById(file.getId()));
        try {
            String text = Farm2FileUtils.readText(filePath.getFile());
            return text;
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @Override
    public GraphPart editGraph(GraphDto graph) {
        GraphPart graphPart = graphPartDao.findById(graph.getId());
        resourceFileServiceImpl.cancel(graphPart.getDatafileid(), Farm2RegisteTypeEnum.GRAPH, graphPart.getId());
        resourceFileServiceImpl.cancel(graphPart.getViewimgid(), Farm2RegisteTypeEnum.GRAPH, graphPart.getId());
        graph.setTitle("图-" + graph.getTitle());
        String title = graph.getTitle().length() > 64 ? graph.getTitle().substring(0, 64) : graph.getTitle();
        ResourceFile jsonFile = resourceFileServiceImpl.saveFile(title + ".json", graph.getDataJson());
        ResourceFile imgFile = resourceFileServiceImpl.saveImgByBase64(title + ".png", graph.getImgBase64());
        //graphPart.setAppid(graph.getAppid());
        //graphPart.setAppmodel(graph.getModel());
        graphPart.setDatafileid(jsonFile.getId());
        graphPart.setViewimgid(imgFile.getId());
        graphPart.setName(title);
        graphPartDao.update(graphPart);
        resourceFileServiceImpl.submit(jsonFile.getId(), Farm2RegisteTypeEnum.GRAPH, graphPart.getId());
        resourceFileServiceImpl.submit(imgFile.getId(), Farm2RegisteTypeEnum.GRAPH, graphPart.getId());
        return graphPart;
    }
}
